<?php 

	class UserDataAccessMySQL implements iUserDataAccess {
		
		
		
		public function buildObject($result) {
			
			$user = new User();
			
			$user->setUserId($result->userId);
			$user->setIdentification($result->identification);
			$user->setIdentificationType($result->identificationType);
			$user->setEmail($result->email);
			$user->setBirthDate(formatDate($result->birthDate));
			$user->setName($result->name);
			$user->setPhone($result->phone);
			
			if (property_exists($result, 'roomGuestId') && property_exists($result, 'employeeId')) {
				$user->setRoomGuestId($result->roomGuestId);
				$user->setEmployeeId($result->employeeId);
			}
			
			return $user;
		}
		
		public function save($user, $dataBaseConnection) {

			$parameters["IDENTIFICATION"] = $user->getIdentification();
			$parameters["IDENTIFICATIONTYPE"] = $user->getIdentificationType();
			$parameters["NAME"] = $user->getName();
			$parameters["BIRTHDATE"] = formatDateMySQL($user->getBirthDate());
			$parameters["PHONE"] = $user->getPhone();
			$parameters["EMAIL"] = $user->getEmail();
			$parameters["PASSWORD"] = $user->getPassword();
			
			$query = buildQuery("queryInsertUser", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
						
					return $dataBaseConnection->getLastInsertId();
				}
				else {
					return -1;
				}
			} else {
				return -1;
			}
			
		}		
		
		public function select($user, $dataBaseConnection) {
			
			$resultArray = array();
			
			if ($user->getUserId() != null){
				$parameters["USERID"] = $user->getUserId();
			}else {
				$parameters["USERID"] = "NULL";
			}
			if ($user->getIdentification() != null){
				$parameters["IDENTIFICATION"] = "'" . $user->getIdentification() . "'";
			}else {
				$parameters["IDENTIFICATION"] = "NULL";
			}
			if ($user->getIdentificationType() != null){
				$parameters["IDENTIFICATIONTYPE"] = "'" . $user->getIdentificationType() . "'";
			}else {
				$parameters["IDENTIFICATIONTYPE"] = "NULL";
			}
			if ($user->getName() != null){
				$parameters["NAME"] = "'" . $user->getName() . "'";
			}else {
				$parameters["NAME"] = "NULL";
			}
			
			if ($user->getPhone() != null){
				$parameters["PHONE"] = "'" . $user->getPhone() . "'";
			}else {
				$parameters["PHONE"] = "NULL";
			}
			if ($user->getEmail() != null){
				$parameters["EMAIL"] = "'" . $user->getEmail() . "'";
			}else {
				$parameters["EMAIL"] = "NULL";
			}
			
			if ($user->getBirthDate() != null){
				$parameters["BIRTHDATE"] = "'" . formatDateMySQL($user->getBirthDate()) . "'";
			}else {
				$parameters["BIRTHDATE"] = "NULL";
			}

			$query = buildQuery("querySelectUser", $parameters);

			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function selectFromSession($user, $dataBaseConnection) {
			
			$parameters["EMAIL"]    = $user->getEmail();
			$parameters["PASSWORD"] = $user->getPassword();
			
			$query = buildQuery("querySelectUserFromSession", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					
					if($row = mysql_fetch_object($result)) {
						return $this->buildObject($row);	
					}else {
						return new User();
					}	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
			
		}
	}

?>